﻿using OA.IRepository;
using OA.IService;
using OA.Repository;
using OA.Repository.Factory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace OA.Service
{
    public abstract class ServiceBase<T>
    {
        public IRepositoryBase<T> repository;

        public abstract void SetRepository();//给 repository 赋值

        public ServiceBase()
        {
            SetRepository();
        }

        public IDbSession session=DbSessionFactory.CreateDbSession();
        public T Add(T entity)
        {
            repository.Add(entity);
            session.SaveChanges();
            return entity;
        }

        public bool Delete(int id)
        {
            repository.Delete(id);
            return session.SaveChanges() > 0; 
        }

        public bool Edit(T entity)
        {
            repository.Update(entity);
            return session.SaveChanges() > 0;
        }

        public T GetEntity(int id)
        {
            return repository.GetEntity(id);
        }

        public IQueryable<T> GetList(Expression<Func<T, bool>> expression)
        {
            return repository.GetList(expression);
        }

        
    }
}
